angular.module('imsApp').controller('creativesCreateAndUpdateController', ['$scope','api', '$rootScope', '$state', 'CreativesManager', 'ImsDialog', 'PromotionGroupManager', 'FileUploader', function ($scope,api, $rootScope, $state, CreativesManager, ImsDialog, PromotionGroupManager, FileUploader) {
    $scope.groupId = $state.params.groupId;
    $scope.campaignsId = $state.params.campaignsId;
    $scope.creativeId = $state.params.creativeId;
    $scope.creativeToSave = {groupId: $scope.groupId, jumpType: 67};

    if($scope.groupId){
        PromotionGroupManager.promotionGroupDetail($scope.groupId).then(function (result) {
            console.log(result)

            $scope.group = result.data;
        });
    }
    if($scope.creativeId){
        CreativesManager.creativesDetail($scope.creativeId).then(function(result){
            $scope.creative=result.data;
            $scope.creativeToSave=result.data.creativeVO;


        });
    }


    $scope.uploaderImage = new FileUploader({
        alias: "photoFile",//后台接收字段
        queueLimit: 2, //文件个数
        url:api+ "/ims/creatives/photo"
    });
    var MAXImage = 1;//单张图片最大大小：单位M
    //获取文件对象
    var image = $scope.uploaderImage = new FileUploader({
        alias: "photoFile",//后台接收字段
        headers: {'X-Auth-Token': $scope.userInfo.token},
        queueLimit: 2, //文件个数
        url: api+"/ims/creatives/photo"
    });
    //图片过滤器
    image.filters.push({
        name: 'photoFile',//过滤器名字
        fn: function (item /*{File|FileLikeObject}*/, options) {
            if (!/\.(jpg|jpeg|png|JPG|PNG)$/.test(item.name)) {
                $scope.checkImage = "E";
                $scope.imageMsg = "图片只能上传png、jpg格式";
                return false;
            }
            if (item.size > MAXImage * 1024 * 1024) {
                $scope.checkImage = "E";
                $scope.imageMsg = "图片须小于" + MAXImage + "MB";
                return false;
            } else {
                $scope.imageMsg =''
                if (this.queue.length == 1) {
                    this.queue[0].remove();
                }
                $scope.checkImage = "R";
                return true;
            }

        }

    });
    //上传图片
    $scope.imageSuccess = false;
    $scope.uploadImage = function () {
        $scope.uploaderImage.uploadAll();
        $scope.uploaderImage.onErrorItem = function (fileItem, response, status, headers) {
            $scope.checkImage = "E";
            $scope.audioMsg = "网络出现故障，上传图片失败";
        };
        $scope.uploaderImage.onProgressItem = function (item, progess) {
        };

        $scope.uploaderImage.onSuccessItem = function (fileItem, response, status, headers) {
            if (response.c == 's') {
                $scope.creativeToSave.picUrl = response.fileVO.webUrl;
                $scope.audioSuccess = true;
                alert('上传成功')
            } else {
                fileItem.isSuccess = false;
                alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
            }
        };
    };
    //获取音频
    var MAXAudio = 5;
    $scope.uploaderAudio = new FileUploader({
        alias: "audioFile",
        headers: {'X-Auth-Token': $scope.userInfo.token},
        queueLimit: 2,     		//文件个数
        url: api+'/ims/creatives/audio',
        filters: [{
            name: 'audioFile',//后台接收字段
            fn: function (item) {
                if (!/\.(mp3||MP3)$/.test(item.name)) {
                    $scope.checkAudio = "E";
                    $scope.audioMsg = "音频只能上传mp3格式";
                    return false;
                }

                if (item.size > MAXAudio * 1024 * 1024) {
                    $scope.checkAudio = "E";
                    $scope.audioMsg = "音频须小于" + MAXAudio + "MB";
                    return false;
                } else {
                    $scope.audioMsg =''
                    if (this.queue.length == 1) {
                        this.queue[0].remove();
                    }
                    $scope.checkAudio = "R";
                    return true;
                }

            }
        }]
    });

    //上传音频
    $scope.audioSuccess = false;
    $scope.uploadAudio = function () {
        $scope.uploaderAudio.uploadAll();
        $scope.uploaderAudio.onErrorItem = function (fileItem, response, status, headers) {
            $scope.checkAudio = "E";
            $scope.audioMsg = "网络出现故障，上传音频失败";
        };
        $scope.uploaderAudio.onProgressItem = function (item, progess) {
        };

        $scope.uploaderAudio.onSuccessItem = function (fileItem, response, status, headers) {
            if (response.c == 's') {
                $scope.creativeToSave.mediaUrl = response.fileVO.webUrl;
                $scope.creativeToSave.duration = response.audioInfoVO.duration;
                $scope.audioSuccess = true;
                alert('上传成功')

            } else {
                fileItem.isSuccess = false;
                alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
            }
        };
    };
    var MAXVideo = 5;
    $scope.uploaderVedio = new FileUploader({
        alias: "vedioFile",
        headers: {'X-Auth-Token': $scope.userInfo.token},
        queueLimit: 2,     		//文件个数
        url: api+'/ims/creatives/video',
        filters: [{
            name: 'vedioFile',//后台接收字段
            fn: function (item, options) {
                if (!/\.(mp4||MP4)$/.test(item.name)) {
                    $scope.checkVideo = "E";
                    $scope.videoMsg = "视频只能上传mp4格式";
                    return false;
                }
                if (item.size > MAXVideo * 1024 * 1024) {
                    $scope.checkVideo = "E";
                    $scope.videoMsg = "音频须小于" + MAXVideo + "MB";
                    return false;
                } else {
                    if (this.queue.length == 1) {
                        this.queue[0].remove();
                    }
                    $scope.checkVideo = "R";
                    return true;
                }

            }
        }],
    });
    //上传视频
    $scope.videoSuccess = false;
    $scope.uploadVideo = function () {
        $scope.uploaderVedio.uploadAll();
        $scope.uploaderVedio.onErrorItem = function (fileItem, response, status, headers) {
            $scope.checkVideo = "E";
            $scope.videoMsg = "网络出现故障，上传视频失败";
        };
        $scope.uploaderVedio.onSuccessItem = function (fileItem, response, headers) {
            if (response.c == "s") {
                $scope.videoMsg='';
                $scope.creativeToSave.duration = response.fileVO.duration;
                $scope.creativeToSave.mediaUrl = response.fileVO.webUrl;
                $scope.videoSuccess = true;
                alert('上传成功')
            } else {
                fileItem.isSuccess = false;
                alert("上传  " + fileItem.file.name + " 失败，请删除后重新添加上传");
            }
        };
    };


    $scope.uploadAll = function () {
        $scope.uploadImage();
        $scope.uploadAudio();
        $scope.uploadVideo();
    }

    $scope.removeFile=function (type) {
        if(type=='image'){
            $scope.creativeToSave.picUrl="";
        }else{
            $scope.creativeToSave.mediaUrl="";
        }

    }
    $scope.cancel = function() {
        $state.go("campaigns.promotionGroup.creatives",{reload:true});
    };
    $scope.submited=false;
    $scope.checkIsPass=true;

    var checkUrl=function(){
        $scope.checkIsPass=true;
        if(($scope.group.title.adStyleId==81||$scope.group.title.adStyleId==82)&&!$scope.creativeToSave.picUrl){
            $scope.checkIsPass=false;
        }else if(($scope.group.title.adStyleId==83||$scope.group.title.adStyleId==84||$scope.group.title.adStyleId==82)&&!$scope.creativeToSave.mediaUrl) {
            $scope.checkIsPass=false;
        }
        return $scope.checkIsPass;
    }
    $scope.submit = function () {
        $scope.submited=true;
        checkUrl();
        if($scope.checkIsPass&&$scope.creativeToSave.creativeName&&$scope.creativeToSave.webUrl){
            CreativesManager.creativesAdd($scope.groupId, $scope.creativeToSave).then(function (result) {
                if (result.data.c == 's') {
                    $state.go('campaigns.promotionGroup.creatives', {groupId: $scope.groupId,campaignsId:$scope.campaignsId}, {
                        inherit: false,
                        reload: true
                    })
                }

            })
        }


    }

    $scope.edit = function (){
        $scope.submited=true;
        if($scope.creativeToSave.webUrl) {
            if ($scope.creativeToSave.creativeName) {
                CreativesManager.creativesEdit($scope.creativeId, $scope.creativeToSave).then(function (result) {
                    if (result.data.c == 's') {
                        $state.go('campaigns.promotionGroup.creatives', {groupId: $scope.groupId,campaignsId:$scope.campaignsId}, {reload: true})
                    }
                })
            }
        }

    }



}])

